package com.loser.utils;

import java.util.Random;

/**
 * @author yzh
 * @desc 订单号生成器
 * @date 2022/6/9
 */
public class IDGeneratorUtil {

    private static volatile IDGeneratorUtil instance;

    private static final Random random = new Random();

    private IDGeneratorUtil() {
    }

    public static IDGeneratorUtil getInstance() {
        if (instance == null) {
            synchronized (IDGeneratorUtil.class) {
                if (instance == null) {
                    instance = new IDGeneratorUtil();
                }
            }
        }
        return instance;
    }

    public String nextOrderID(String userId) {
        return userId + System.currentTimeMillis();
    }

    /**
     * 获得一个订单ID
     */
    public String nextOrderId(int type, String userId) {
        String typeStr = "00" + type;
        String userIdStr = "00000000" + userId;
        return typeStr.substring(typeStr.length() - 2)
                + userIdStr.substring(userIdStr.length() - 8)
                + System.currentTimeMillis() + (random.nextInt(9000) + 1000);
    }

}
